package com.zzh.bc.login.controller;

import com.zzh.bc.dao.login.LoginDao;
import com.zzh.bc.dao.login.impl.LoginDaoImp;
import com.zzh.bc.db.C3p0Utils;
import com.zzh.bc.db.MySQLExecute;
import com.zzh.bc.login.vo.LoginVo;
import com.zzh.bc.model.UserModel;
import com.zzh.bc.utils.RequestParamUtils;
import com.zzh.bc.utils.RespParamUtils;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;


import java.io.IOException;

@WebServlet(name = "LoginController")
public class LoginController extends HttpServlet {
    private LoginDao mLoginDao;
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        LoginVo loginVo = RequestParamUtils.requestBodyParams(request, LoginVo.class);
        if (loginVo == null) {
            return;
        }
        if (mLoginDao == null) {
            mLoginDao = new LoginDaoImp();
        }
        UserModel model = mLoginDao.findUserByAccountAndPassword(loginVo.getAccount(), loginVo.getPassword());
        if (model == null) {
            RespParamUtils.respJsonError(response, "账号或密码错误");
        } else {
            RespParamUtils.respJsonOk(response, model);
        }
    }
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
